package snowflake

import (
	"fmt"
	"time"

	"github.com/bwmarrin/snowflake"
)

var node *snowflake.Node

func _init(startTime string, machineID int64) (err error) {
	var st time.Time
	st, err = time.Parse("2006-01-02", startTime)
	if err != nil {
		fmt.Println(err)
		return
	}

	snowflake.Epoch = st.UnixNano() / 1e6
	node, err = snowflake.NewNode(machineID)
	if err != nil {
		fmt.Println(err)
		return
	}

	return
}

// GetSysOnlyId GenID 生成 64 位的 雪花 ID
func GetSysOnlyId() int64 {
	return node.Generate().Int64()
}

func Start() {
	err := _init(time.Now().Format("2006-01-02"), 21)
	if err != nil {
		panic("启动雪花ID生成器失败: " + err.Error())
	}
}
